package cn.ecnu.service.impl;

import cn.ecnu.common.constant.BusinessConstant;
import cn.ecnu.mapper.DutyMapper;
import cn.ecnu.mapper.UserMapper;
import cn.ecnu.pojo.Duty;
import cn.ecnu.pojo.User;
import cn.ecnu.service.DutyService;
import cn.ecnu.vo.fromFront.AddDutyVo;
import cn.ecnu.vo.fromFront.ChangeDefaultDutyVo;
import cn.ecnu.vo.fromFront.DeleteDutyVo;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Locale;

@Service
public class DutyServiceImpl implements DutyService {

    @Resource
    private DutyMapper dutyMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public void deleteDuty(DeleteDutyVo vo) {
        try {
            dutyMapper.deleteDutyById(vo.getDutyId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 0 - 1月
    @Override
    public void createDefaultDuty(int year, int month) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        SetOperations<Object, Object> setOps = redisTemplate.opsForSet();
        calendar.set(year, month - 1, 1); // 这个月的第一天
        while (true) {
            int day = calendarDayToDay(calendar.get(Calendar.DAY_OF_WEEK));
            String key1 = String.format(BusinessConstant.CONSULTANT_DEFAULT_DUTY_KEY, day);
            String key2 = String.format(BusinessConstant.SUPERVISOR_DEFAULT_DUTY_KEY, day);
            long size1 = setOps.size(key1);
            long size2 = setOps.size(key2);
            if (size1 > 0) {
                setOps.members(key1).forEach((id) -> {
                    Duty duty = new Duty();
                    duty.setUserId((Integer) id);
                    duty.setDutyDate(calendar.getTime());
                    dutyMapper.addDuty(duty);
                });
            }

            if (size2 > 0) {
                setOps.members(key2).forEach((id) -> {
                    Duty duty = new Duty();
                    duty.setUserId((Integer) id);
                    duty.setDutyDate(calendar.getTime());
                    dutyMapper.addDuty(duty);
                });
            }
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            if (calendar.get(Calendar.MONTH) == month) break;
        }
    }

    int calendarDayToDay(int day) {
        if (day > Calendar.SUNDAY) return day - 1;
        return 7;
    }

    @Override
    public void addDuty(AddDutyVo vo) {
        try {
            Duty duty = new Duty();
            duty.setDutyDate(vo.getDate());
            duty.setUserId(vo.getUserId());
            dutyMapper.addDuty(duty);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void addDefaultDuty(ChangeDefaultDutyVo vo) {
        try {
            String template = BusinessConstant.SUPERVISOR_DEFAULT_DUTY_KEY;
            User user = userMapper.getUserById(vo.getUserId());
            if (user.getRole() == 1) template = BusinessConstant.CONSULTANT_DEFAULT_DUTY_KEY;
            SetOperations<Object, Object> opsForSet = redisTemplate.opsForSet();
            for (Integer day : vo.getDefaultDuty()) {
                String key = String.format(template, day);
                opsForSet.add(key, vo.getUserId());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
